package com.bawei.a2010douyin.ui.tuijian;

import android.util.Log;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;
import com.bawei.a2010douyin.entity.BaseResponse;
import com.bawei.a2010douyin.entity.CommentEntity;
import com.bawei.a2010douyin.entity.VideoEntity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class TuiJianPresenter extends BasePresenter<TuiJianContract.ITuiJianView> implements TuiJianContract.ITuiJianPresenter {
    private TuiJianContract.ITuiJianModel mITuiJianModel;
    public TuiJianPresenter() {
        mITuiJianModel = new TuiJianModel();
    }
    @Override
    public void video(int currentPage, int pageSize) {
        mITuiJianModel.video(currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<VideoEntity>>())//如何编写:
                .subscribe(new Observer<List<VideoEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<VideoEntity> videoEntities) {//200
                        if(isAttach()){
                            getView().showVideo(videoEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {//!=200
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void love(int videoId) {
        mITuiJianModel.love(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<String>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(String str) {//点赞成功
                        Log.d("TAG", "love11: ");
                        if(isAttach()){
                            getView().love(str);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void nolove(int videoId) {
        mITuiJianModel.nolove(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<String>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(String data) {//取消点赞成功
                        Log.d("TAG", "nolove11: ");
                        if(isAttach()){
                            getView().nolove(data);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void guanzhu(RequestBody requestBody) {
        mITuiJianModel.guanzhu(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<String>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(String s) {
                        if(isAttach()){
                            getView().guanzhu(s);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void comment(int videoId) {
        mITuiJianModel.comment(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<CommentEntity>>())
                .subscribe(new Observer<List<CommentEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<CommentEntity> data) {
                        if(isAttach()){
                            getView().showComment(data);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void sendComment(RequestBody requestBody) {
        mITuiJianModel.sendComment(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<CommentEntity>>())
                .subscribe(new Observer<List<CommentEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<CommentEntity> data) {
                        if(isAttach()){
                            getView().showComment(data);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
